<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Anderoid基础," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="封装SharedPreferences1234567891011121314151617181920212223242526272829303132333435模版：        public class SpUtil &amp;#123;    private static SharedPreferences sp;    /**     *     * @param ctx 上下文环境     *">
<meta name="keywords" content="Anderoid基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Android基础（二）">
<meta property="og:url" content="http://yoursite.com/2016/11/30/Android基础（二）/index.html">
<meta property="og:site_name" content="JokerLiang">
<meta property="og:description" content="封装SharedPreferences1234567891011121314151617181920212223242526272829303132333435模版：        public class SpUtil &amp;#123;    private static SharedPreferences sp;    /**     *     * @param ctx 上下文环境     *">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-11-30T11:13:51.620Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android基础（二）">
<meta name="twitter:description" content="封装SharedPreferences1234567891011121314151617181920212223242526272829303132333435模版：        public class SpUtil &amp;#123;    private static SharedPreferences sp;    /**     *     * @param ctx 上下文环境     *">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/11/30/Android基础（二）/"/>





  <title> Android基础（二） | JokerLiang </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  








  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1260895995&web_id=1260895995" language="JavaScript"></script>
  </div>





  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">JokerLiang</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">永远年轻，永远热泪盈眶。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/11/30/Android基础（二）/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="JokerLiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/me.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="JokerLiang">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="JokerLiang" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Android基础（二）
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-11-30T12:46:36+08:00">
                2016-11-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index">
                    <span itemprop="name">Android</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="&#x5C01;&#x88C5;SharedPreferences"><a href="#&#x5C01;&#x88C5;SharedPreferences" class="headerlink" title="&#x5C01;&#x88C5;SharedPreferences"></a>&#x5C01;&#x88C5;SharedPreferences</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">&#x6A21;&#x7248;&#xFF1A;</span><br><span class="line">        <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpUtil</span> </span>{</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> SharedPreferences sp;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ctx &#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key   &#x5B58;&#x50A8;&#x8282;&#x70B9;&#x7684;&#x540D;&#x79F0;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> value &#x5B58;&#x50A8;&#x8282;&#x70B9;&#x7684;&#x503C;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//&#x5199;</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">PutBoolean</span> <span class="params">(Context ctx , String key ,Boolean value)</span></span>{</span><br><span class="line">        <span class="keyword">if</span>(sp==<span class="keyword">null</span>){</span><br><span class="line">            <span class="comment">//&#x8FD9;&#x91CC;&#x7684;&#x4E24;&#x4E2A;&#x53C2;&#x6570; &#x7B2C;&#x4E00;&#x4E2A;&#x662F; &#x5B58;&#x50A8;&#x6587;&#x4EF6;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x662F;&#x8BFB;&#x53D6;&#x65B9;&#x5F0F;&#xFF0C;&#x4E00;&#x822C;&#x662F;Context.MODE_APPEND</span></span><br><span class="line">            sp = ctx.getSharedPreferences(<span class="string">&quot;config&quot;</span>, Context.MODE_APPEND);</span><br><span class="line">        }</span><br><span class="line">        sp.edit().putBoolean(key,value).commit();</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> ctx &#x4E0A;&#x4E0B;&#x6587;&#x73AF;&#x5883;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key &#x5B58;&#x50A8;&#x8282;&#x70B9;&#x7684;&#x540D;&#x79F0;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> deftvalue &#x5B58;&#x50A8;&#x8282;&#x70B9;&#x7684;&#x9ED8;&#x8BA4;&#x503C;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> &#x9ED8;&#x8BA4;&#x503C;&#x6216;&#x8005;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x503C;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//&#x8BFB;</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">getBoolean</span><span class="params">(Context ctx, String key, Boolean deftvalue)</span></span>{</span><br><span class="line">        <span class="keyword">if</span>(sp==<span class="keyword">null</span>){</span><br><span class="line">            <span class="comment">//&#x8FD9;&#x91CC;&#x7684;&#x4E24;&#x4E2A;&#x53C2;&#x6570; &#x7B2C;&#x4E00;&#x4E2A;&#x662F; &#x5B58;&#x50A8;&#x6587;&#x4EF6;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x662F;&#x8BFB;&#x53D6;&#x65B9;&#x5F0F;&#xFF0C;&#x4E00;&#x822C;&#x662F;Context.MODE_APPEND</span></span><br><span class="line">            sp = ctx.getSharedPreferences(<span class="string">&quot;config&quot;</span>, Context.MODE_APPEND);</span><br><span class="line">        }</span><br><span class="line">        <span class="keyword">return</span> sp.getBoolean(key,deftvalue);</span><br><span class="line">    }</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="&#x5C06;&#x6D41;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x5728;&#x89E3;&#x6790;JSON&#x5904;-&#x4E0D;&#x9002;&#x7528;Gson&#xFF09;"><a href="#&#x5C06;&#x6D41;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x5728;&#x89E3;&#x6790;JSON&#x5904;-&#x4E0D;&#x9002;&#x7528;Gson&#xFF09;" class="headerlink" title="&#x5C06;&#x6D41;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x5728;&#x89E3;&#x6790;JSON&#x5904;,&#x4E0D;&#x9002;&#x7528;Gson&#xFF09;"></a>&#x5C06;&#x6D41;&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x5728;&#x89E3;&#x6790;JSON&#x5904;,&#x4E0D;&#x9002;&#x7528;Gson&#xFF09;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">        <span class="comment">//&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;&#x5C06;&#x6D41;&#x7C7B;&#x578B;&#x8F6C;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StreamUtil</span> </span>{</span><br><span class="line"></span><br><span class="line">    <span class="comment">//&#x524D;&#x9762;&#x7C7B;&#x7684;&#x4E34;&#x65F6;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#x4F20;&#x8FDB;&#x6765;</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">streamToString</span><span class="params">(InputStream is)</span> </span>{</span><br><span class="line">        <span class="comment">//1,&#x5728;&#x8BFB;&#x53D6;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;,&#x5C06;&#x8BFB;&#x53D6;&#x7684;&#x5185;&#x5BB9;&#x5B58;&#x50A8;&#x503C;&#x7F13;&#x5B58;&#x4E2D;,&#x7136;&#x540E;&#x4E00;&#x6B21;&#x6027;&#x7684;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x8FD4;&#x56DE;</span></span><br><span class="line">        ByteArrayOutputStream bos = <span class="keyword">new</span> ByteArrayOutputStream();</span><br><span class="line">        <span class="comment">//2,&#x8BFB;&#x6D41;&#x64CD;&#x4F5C;,&#x8BFB;&#x5230;&#x6CA1;&#x6709;&#x4E3A;&#x6B62;(&#x5FAA;&#x73AF;)</span></span><br><span class="line">        <span class="keyword">byte</span>[] buffer = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="comment">//3,&#x8BB0;&#x5F55;&#x8BFB;&#x53D6;&#x5185;&#x5BB9;&#x7684;&#x4E34;&#x65F6;&#x53D8;&#x91CF;</span></span><br><span class="line">        <span class="keyword">int</span> temp = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            <span class="keyword">while</span>((temp = is.read(buffer))!=-<span class="number">1</span>){</span><br><span class="line">                bos.write(buffer, <span class="number">0</span>, temp);</span><br><span class="line">            }</span><br><span class="line">            <span class="comment">//&#x8FD4;&#x56DE;&#x8BFB;&#x53D6;&#x6570;&#x636E;</span></span><br><span class="line">            <span class="keyword">return</span> bos.toString();</span><br><span class="line">        } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        }<span class="keyword">finally</span>{</span><br><span class="line">            <span class="keyword">try</span> {</span><br><span class="line">                is.close();</span><br><span class="line">                bos.close();</span><br><span class="line">            } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            }</span><br><span class="line">        }</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="&#x5224;&#x65AD;&#x670D;&#x52A1;&#x662F;&#x5426;&#x8FD0;&#x884C;"><a href="#&#x5224;&#x65AD;&#x670D;&#x52A1;&#x662F;&#x5426;&#x8FD0;&#x884C;" class="headerlink" title="&#x5224;&#x65AD;&#x670D;&#x52A1;&#x662F;&#x5426;&#x8FD0;&#x884C;"></a>&#x5224;&#x65AD;&#x670D;&#x52A1;&#x662F;&#x5426;&#x8FD0;&#x884C;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">      <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServiceUtil</span> </span>{</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">boolean</span> <span class="title">isRunning</span><span class="params">(Context context,String name)</span></span>{</span><br><span class="line">      ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);</span><br><span class="line">        <span class="comment">//&#x83B7;&#x53D6;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7684;&#x670D;&#x52A1;&#x540D;&#x5B57;&#xFF0C;&#x540E;&#x9762;&#x7684;1000&#x8868;&#x793A; &#x80FD;&#x83B7;&#x53D6;&#x7684;&#x6700;&#x591A;&#x7684;&#x670D;&#x52A1;&#x6570;&#x91CF;</span></span><br><span class="line">        List&lt;ActivityManager.RunningServiceInfo&gt; runningServices = activityManager.getRunningServices(<span class="number">1000</span>);</span><br><span class="line">        <span class="keyword">for</span>(ActivityManager.RunningServiceInfo runningServices1 : runningServices ){</span><br><span class="line">            <span class="comment">//&#x83B7;&#x53D6;&#x670D;&#x52A1;&#x540D;&#x79F0;</span></span><br><span class="line">            String ServiceName = runningServices1.service.getClassName();</span><br><span class="line">            <span class="keyword">if</span> (ServiceName.equals(name)){</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            }</span><br><span class="line">        }</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="&#x4EFF;&#x5E7F;&#x544A;&#x8F6E;&#x64AD;&#x56FE;&#xFF0C;ViewPager&#x7684;&#x5E94;&#x7528;"><a href="#&#x4EFF;&#x5E7F;&#x544A;&#x8F6E;&#x64AD;&#x56FE;&#xFF0C;ViewPager&#x7684;&#x5E94;&#x7528;" class="headerlink" title="&#x4EFF;&#x5E7F;&#x544A;&#x8F6E;&#x64AD;&#x56FE;&#xFF0C;ViewPager&#x7684;&#x5E94;&#x7528;"></a>&#x4EFF;&#x5E7F;&#x544A;&#x8F6E;&#x64AD;&#x56FE;&#xFF0C;ViewPager&#x7684;&#x5E94;&#x7528;</h2><h3 id="&#x51C6;&#x5907;&#x6570;&#x636E;"><a href="#&#x51C6;&#x5907;&#x6570;&#x636E;" class="headerlink" title="&#x51C6;&#x5907;&#x6570;&#x636E;"></a>&#x51C6;&#x5907;&#x6570;&#x636E;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">      <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initData</span><span class="params">()</span> </span>{</span><br><span class="line">    <span class="comment">// &#x521D;&#x59CB;&#x5316;&#x8981;&#x663E;&#x793A;&#x7684;&#x6570;&#x636E;        </span></span><br><span class="line">    <span class="comment">// &#x56FE;&#x7247;&#x8D44;&#x6E90;id&#x6570;&#x7EC4;</span></span><br><span class="line">    imageResIds = <span class="keyword">new</span> <span class="keyword">int</span>[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};        </span><br><span class="line">    <span class="comment">// &#x521D;&#x59CB;&#x5316;&#x8981;&#x5C55;&#x793A;&#x7684;5&#x4E2A;ImageView</span></span><br><span class="line">    imageViewList = <span class="keyword">new</span> ArrayList&lt;ImageView&gt;();        </span><br><span class="line">    ImageView imageView;</span><br><span class="line">    View pointView;</span><br><span class="line">    LayoutParams layoutParams;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; imageResIds.length; i++) {</span><br><span class="line">        <span class="comment">// &#x521D;&#x59CB;&#x5316;&#x8981;&#x663E;&#x793A;&#x7684;&#x56FE;&#x7247;&#x5BF9;&#x8C61;</span></span><br><span class="line">        imageView = <span class="keyword">new</span> ImageView(<span class="keyword">this</span>);</span><br><span class="line">        imageView.setBackgroundResource(imageResIds[i]);</span><br><span class="line">        imageViewList.add(imageView);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// &#x52A0;&#x5C0F;&#x767D;&#x70B9;, &#x6307;&#x793A;&#x5668;</span></span><br><span class="line">        pointView = <span class="keyword">new</span> View(<span class="keyword">this</span>);</span><br><span class="line">        pointView.setBackgroundResource(R.drawable.selector_bg_point);</span><br><span class="line">        layoutParams = <span class="keyword">new</span> LinearLayout.LayoutParams(<span class="number">5</span>, <span class="number">5</span>);</span><br><span class="line">        <span class="keyword">if</span>(i != <span class="number">0</span>){</span><br><span class="line">            layoutParams.leftMargin = <span class="number">10</span>;</span><br><span class="line">        }</span><br><span class="line">        <span class="comment">// &#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x6240;&#x6709;&#x90FD;&#x4E0D;&#x53EF;&#x7528;</span></span><br><span class="line">        pointView.setEnabled(<span class="keyword">false</span>);</span><br><span class="line">        ll_point_container.addView(pointView, layoutParams);</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x5199;&#x51FA;&#x8BBE;&#x914D;&#x5668;"><a href="#&#x5199;&#x51FA;&#x8BBE;&#x914D;&#x5668;" class="headerlink" title="&#x5199;&#x51FA;&#x8BBE;&#x914D;&#x5668;"></a>&#x5199;&#x51FA;&#x8BBE;&#x914D;&#x5668;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">      <span class="class"><span class="keyword">class</span> <span class="title">MyAdapter</span> <span class="keyword">extends</span> <span class="title">PagerAdapter</span></span>{</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> Integer.MAX_VALUE;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. &#x6307;&#x5B9A;&#x590D;&#x7528;&#x7684;&#x5224;&#x65AD;&#x903B;&#x8F91;, &#x56FA;&#x5B9A;&#x5199;&#x6CD5;</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isViewFromObject</span><span class="params">(View view, Object object)</span> </span>{</span><br><span class="line">        <span class="comment">// &#x5F53;&#x5212;&#x5230;&#x65B0;&#x7684;&#x6761;&#x76EE;, &#x53C8;&#x8FD4;&#x56DE;&#x6765;, view&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x88AB;&#x590D;&#x7528;.</span></span><br><span class="line">        <span class="comment">// &#x8FD4;&#x56DE;&#x5224;&#x65AD;&#x89C4;&#x5219;</span></span><br><span class="line">        <span class="keyword">return</span> view == object;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. &#x8FD4;&#x56DE;&#x8981;&#x663E;&#x793A;&#x7684;&#x6761;&#x76EE;&#x5185;&#x5BB9;, &#x521B;&#x5EFA;&#x6761;&#x76EE;</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">instantiateItem</span><span class="params">(ViewGroup container, <span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">        System.out.println(<span class="string">&quot;instantiateItem&#x521D;&#x59CB;&#x5316;: &quot;</span> + position);</span><br><span class="line">        <span class="comment">// container: &#x5BB9;&#x5668;: ViewPager</span></span><br><span class="line">        <span class="comment">// position: &#x5F53;&#x524D;&#x8981;&#x663E;&#x793A;&#x6761;&#x76EE;&#x7684;&#x4F4D;&#x7F6E; 0 -&gt; 4            </span></span><br><span class="line">        <span class="keyword">int</span> newPosition = position % imageViewList.size();            </span><br><span class="line">        ImageView imageView = imageViewList.get(newPosition);</span><br><span class="line">        <span class="comment">// a. &#x628A;View&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5230;container&#x4E2D;</span></span><br><span class="line">        container.addView(imageView);</span><br><span class="line">        <span class="comment">// b. &#x628A;View&#x5BF9;&#x8C61;&#x8FD4;&#x56DE;&#x7ED9;&#x6846;&#x67B6;, &#x9002;&#x914D;&#x5668;</span></span><br><span class="line">        <span class="keyword">return</span> imageView; <span class="comment">// &#x5FC5;&#x987B;&#x91CD;&#x5199;, &#x5426;&#x5219;&#x62A5;&#x5F02;&#x5E38;</span></span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. &#x9500;&#x6BC1;&#x6761;&#x76EE;</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroyItem</span><span class="params">(ViewGroup container, <span class="keyword">int</span> position, Object object)</span> </span>{</span><br><span class="line">        <span class="comment">// object &#x8981;&#x9500;&#x6BC1;&#x7684;&#x5BF9;&#x8C61;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;destroyItem&#x9500;&#x6BC1;: &quot;</span> + position);</span><br><span class="line">        container.removeView((View)object);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x6DFB;&#x52A0;&#x6EDA;&#x52A8;&#x4E8B;&#x4EF6;"><a href="#&#x6DFB;&#x52A0;&#x6EDA;&#x52A8;&#x4E8B;&#x4EF6;" class="headerlink" title="&#x6DFB;&#x52A0;&#x6EDA;&#x52A8;&#x4E8B;&#x4EF6;"></a>&#x6DFB;&#x52A0;&#x6EDA;&#x52A8;&#x4E8B;&#x4EF6;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">      viewPager.setOnPageChangeListener(<span class="keyword">new</span> OnPageChangeListener);<span class="comment">// &#x8BBE;&#x7F6E;&#x9875;&#x9762;&#x66F4;&#x65B0;&#x76D1;&#x542C;</span></span><br><span class="line">          <span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageScrolled</span><span class="params">(<span class="keyword">int</span> position, <span class="keyword">float</span> positionOffset,</span></span></span><br><span class="line"><span class="function"><span class="params">        <span class="keyword">int</span> positionOffsetPixels)</span> </span>{</span><br><span class="line">    <span class="comment">// &#x6EDA;&#x52A8;&#x65F6;&#x8C03;&#x7528;</span></span><br><span class="line">}</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageSelected</span><span class="params">(<span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">    <span class="comment">// &#x65B0;&#x7684;&#x6761;&#x76EE;&#x88AB;&#x9009;&#x4E2D;&#x65F6;&#x8C03;&#x7528;</span></span><br><span class="line">    System.out.println(<span class="string">&quot;onPageSelected: &quot;</span> + position);</span><br><span class="line">    <span class="keyword">int</span> newPosition = position % imageViewList.size();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//&#x8BBE;&#x7F6E;&#x6587;&#x672C;</span></span><br><span class="line">    tv_desc.setText(contentDescs[newPosition]);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// &#x628A;&#x4E4B;&#x524D;&#x7684;&#x7981;&#x7528;, &#x628A;&#x6700;&#x65B0;&#x7684;&#x542F;&#x7528;, &#x66F4;&#x65B0;&#x6307;&#x793A;&#x5668;</span></span><br><span class="line">    ll_point_container.getChildAt(previousSelectedPosition).setEnabled(<span class="keyword">false</span>);</span><br><span class="line">    ll_point_container.getChildAt(newPosition).setEnabled(<span class="keyword">true</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// &#x8BB0;&#x5F55;&#x4E4B;&#x524D;&#x7684;&#x4F4D;&#x7F6E;</span></span><br><span class="line">    previousSelectedPosition  = newPosition;</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPageScrollStateChanged</span><span class="params">(<span class="keyword">int</span> state)</span> </span>{</span><br><span class="line">    <span class="comment">// &#x6EDA;&#x52A8;&#x72B6;&#x6001;&#x53D8;&#x5316;&#x65F6;&#x8C03;&#x7528;</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="&#x5728;&#x5B50;&#x7EBF;&#x7A0B;&#x4E2D;&#x5B9E;&#x73B0;&#x81EA;&#x52A8;&#x8F6E;&#x64AD;"><a href="#&#x5728;&#x5B50;&#x7EBF;&#x7A0B;&#x4E2D;&#x5B9E;&#x73B0;&#x81EA;&#x52A8;&#x8F6E;&#x64AD;" class="headerlink" title="&#x5728;&#x5B50;&#x7EBF;&#x7A0B;&#x4E2D;&#x5B9E;&#x73B0;&#x81EA;&#x52A8;&#x8F6E;&#x64AD;"></a>&#x5728;&#x5B50;&#x7EBF;&#x7A0B;&#x4E2D;&#x5B9E;&#x73B0;&#x81EA;&#x52A8;&#x8F6E;&#x64AD;</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">            <span class="comment">// &#x5F00;&#x542F;&#x8F6E;&#x8BE2;</span></span><br><span class="line"><span class="keyword">new</span> Thread(){</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>{</span><br><span class="line">        isRunning = <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">while</span>(isRunning){</span><br><span class="line">            <span class="keyword">try</span> {</span><br><span class="line">                Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            } <span class="keyword">catch</span> (InterruptedException e) {</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            }</span><br><span class="line">            <span class="comment">// &#x5F80;&#x4E0B;&#x8DF3;&#x4E00;&#x4F4D;</span></span><br><span class="line">            runOnUiThread(<span class="keyword">new</span> Runnable() {                        </span><br><span class="line">                <span class="meta">@Override</span></span><br><span class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>{</span><br><span class="line">                    viewPager.setCurrentItem(viewPager.getCurrentItem()+<span class="number">1</span>);                            </span><br><span class="line">                }</span><br><span class="line">            });</span><br><span class="line">        }</span><br><span class="line">    };</span><br><span class="line">}.start();</span><br></pre></td></tr></table></figure>
<h2 id="Handler&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x7528;&#x6CD5;"><a href="#Handler&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x7528;&#x6CD5;" class="headerlink" title="Handler&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x7528;&#x6CD5;"></a>Handler&#x5B9A;&#x65F6;&#x5668;&#x7684;&#x7528;&#x6CD5;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x53C2;&#x6570;&#xFF1A;(&#x60F3;&#x8981;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x5EF6;&#x8FDF;&#x7684;&#x65F6;&#x95F4;)</span><br><span class="line">         <span class="keyword">new</span> Handler().postDelayed(<span class="keyword">new</span> Runnable() {</span><br><span class="line">          <span class="meta">@Override</span></span><br><span class="line">          <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>{</span><br><span class="line">              startActivity(<span class="keyword">new</span> Intent(Splish_Activity.<span class="keyword">this</span>,Main_Activity.class));</span><br><span class="line">              finish();</span><br><span class="line">          }</span><br><span class="line">      },<span class="number">2000</span>);</span><br></pre></td></tr></table></figure>
<h2 id="TabLayout&#x5B9E;&#x73B0;&#x6807;&#x9898;&#x680F;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;"><a href="#TabLayout&#x5B9E;&#x73B0;&#x6807;&#x9898;&#x680F;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;" class="headerlink" title="TabLayout&#x5B9E;&#x73B0;&#x6807;&#x9898;&#x680F;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;"></a>TabLayout&#x5B9E;&#x73B0;&#x6807;&#x9898;&#x680F;&#x7684;&#x7B80;&#x5355;&#x4F7F;&#x7528;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>;&#x5728;&#x5E03;&#x5C40;&#x6587;&#x4EF6;&#x4E2D;&#x7533;&#x660E;,&#x5E76;&#x5728;TabLayout&#x4E0B;&#x9762;&#x653E;ViewPager</span><br><span class="line">          &lt;android.support.design.widget.TabLayout</span><br><span class="line">        android:id=<span class="string">&quot;@+id/Tablayout1&quot;</span></span><br><span class="line">        app:tabMode=<span class="string">&quot;scrollable&quot;</span></span><br><span class="line">        app:tabSelectedTextColor=<span class="string">&quot;@color/colorPrimary&quot;</span></span><br><span class="line">        app:tabIndicatorColor=<span class="string">&quot;@color/colorPrimary&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;wrap_content&quot;</span></span><br><span class="line">        /&gt;</span><br><span class="line">         &lt;android.support.v4.view.ViewPager</span><br><span class="line">        android:id=<span class="string">&quot;@+id/vp_main&quot;</span></span><br><span class="line">        android:layout_width=<span class="string">&quot;match_parent&quot;</span></span><br><span class="line">        android:layout_height=<span class="string">&quot;0dp&quot;</span></span><br><span class="line">        android:layout_weight = <span class="string">&quot;1&quot;</span></span><br><span class="line">        /&gt;</span><br><span class="line">  <span class="number">2</span>:&#x5728;JAVA&#x6587;&#x4EF6;&#x4E2D;&#x627E;&#x51FA;&#x63A7;&#x4EF6;</span><br><span class="line">      mTablayout = (TabLayout) findViewById(R.id.Tablayout1);</span><br><span class="line">      mVp_main = (ViewPager) findViewById(R.id.vp_main);</span><br><span class="line">  <span class="number">3</span>:&#x4E3A;ViewPager&#x8BBE;&#x7F6E;&#x9002;&#x914D;&#x5668;&#xFF0C;&#x9002;&#x914D;&#x5668;&#x5E94;&#x8BE5;&#x7EE7;&#x627F;FragmentPagerAdapter&#xFF0C;&#x5927;&#x81F4;&#x548C;&#x666E;&#x901A;&#x7684;ViewPager &#x9002;&#x914D;&#x5668;&#x5DEE;&#x4E0D;&#x591A;&#xFF0C;&#x591A;&#x4E86;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x6BCF;&#x4E2A;ViewPager &#x9875;&#x9762;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F;&#x4E00;&#x4E2A;Fragment&#x5BF9;&#x8C61;&#x3002;</span><br><span class="line">         <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainTabAdapter</span> <span class="keyword">extends</span> <span class="title">FragmentPagerAdapter</span> </span>{</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> List&lt;FirstPagerFragment&gt; mList_fragment ;</span><br><span class="line">    <span class="keyword">private</span> String[] mList_titlt;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MainTabAdapter</span><span class="params">(FragmentManager fm,List&lt;FirstPagerFragment&gt; List_fragment,String[] List_titlt)</span> </span>{</span><br><span class="line">        <span class="keyword">super</span>(fm);</span><br><span class="line">        mList_fragment = List_fragment ;</span><br><span class="line">        mList_titlt = List_titlt;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> FirstPagerFragment <span class="title">getItem</span><span class="params">(<span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> mList_fragment.get(position);</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> mList_fragment.size();</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    * &#x8FD9;&#x4E2A;&#x662F;&#x8BBE;&#x7F6E;&#x9875;&#x7B7E;&#x6807;&#x9898;*/</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> CharSequence <span class="title">getPageTitle</span><span class="params">(<span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">        <span class="keyword">return</span> mList_titlt[position] ;</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line">  <span class="number">4</span>:&#x8BBE;&#x7F6E;&#x9002;&#x914D;&#x5668;&#x540E;&#x5C06;TabLayout&#x548C;ViewPager&#x7ED1;&#x5B9A;&#x8D77;&#x6765;</span><br><span class="line">        <span class="comment">//&#x4E3A;ViewPager&#x8BBE;&#x7F6E;&#x9002;&#x914D;&#x5668;</span></span><br><span class="line">        mVp_main.setAdapter(mTabAdapter);</span><br><span class="line">        <span class="comment">//&#x5C06;TabLayout &#x548C;ViewPager  &#x7ED1;&#x5B9A;&#x8D77;&#x6765;</span></span><br><span class="line">        mTablayout.setupWithViewPager(mVp_main);</span><br></pre></td></tr></table></figure>
<h2 id="RecyclerView&#x9002;&#x914D;&#x5668;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;"><a href="#RecyclerView&#x9002;&#x914D;&#x5668;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;" class="headerlink" title="RecyclerView&#x9002;&#x914D;&#x5668;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;"></a>RecyclerView&#x9002;&#x914D;&#x5668;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> TYPE_HEAD = <span class="number">0</span> ;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">int</span> TYPE_NORMAL = <span class="number">1</span> ;</span><br><span class="line"><span class="keyword">private</span> Context mContext;</span><br><span class="line"><span class="keyword">private</span> List&lt;JsonData&gt; mitem_data ; <span class="comment">//&#x6B63;&#x5E38;item &#x7684;&#x6570;&#x636E;</span></span><br><span class="line"><span class="keyword">private</span> BannerBean mBannerBean ;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">FirstPagerFragmetAdapter</span><span class="params">(Context context, List&lt;JsonData&gt; item_data, BannerBean bannerBean)</span></span>{</span><br><span class="line">    mContext = context;</span><br><span class="line">    mitem_data = item_data;</span><br><span class="line">    mBannerBean = bannerBean ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//&#x521B;&#x5EFA;&#x5BF9;&#x5E94;&#x7684;ViewHolder</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> RecyclerView.<span class="function">ViewHolder <span class="title">onCreateViewHolder</span><span class="params">(ViewGroup parent, <span class="keyword">int</span> viewType)</span> </span>{</span><br><span class="line">    RecyclerView.ViewHolder  holder = <span class="keyword">null</span> ;</span><br><span class="line">    <span class="keyword">if</span> (viewType == TYPE_HEAD){</span><br><span class="line">        <span class="comment">//&#x8868;&#x660E;&#x521B;&#x5EFA;&#x5934;&#x90E8;Banner</span></span><br><span class="line">        holder = <span class="keyword">new</span> BannerHolder(LayoutInflater.from(mContext).inflate(R.layout.item_banner,</span><br><span class="line">                parent,<span class="keyword">false</span>));</span><br><span class="line">    }<span class="keyword">else</span> {</span><br><span class="line">        holder = <span class="keyword">new</span> ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.itm_normal,</span><br><span class="line">                parent,<span class="keyword">false</span>)) ;</span><br><span class="line">    }</span><br><span class="line">    <span class="keyword">return</span> holder;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//&#x7ED1;&#x5B9A;ViewHolder&#xFF0C;&#x5E76;&#x8BBE;&#x7F6E;&#x76F8;&#x5173;&#x6570;&#x636E;</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onBindViewHolder</span><span class="params">(RecyclerView.ViewHolder holder, <span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">    <span class="keyword">if</span> (holder <span class="keyword">instanceof</span>  BannerHolder){</span><br><span class="line">        BannerHolder Bholder = (BannerHolder) holder;</span><br><span class="line">        Bholder.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);</span><br><span class="line">        Bholder.banner.setBannerTitle(mBannerBean.getTitlt());</span><br><span class="line">        Bholder.banner.setImages(mBannerBean.getImg_url());</span><br><span class="line">        System.out.println(mBannerBean.getImg_url()+<span class="string">&quot;----&gt;Banner&#x8DEF;&#x5F84;&quot;</span>);</span><br><span class="line">    }<span class="keyword">else</span> <span class="keyword">if</span> (holder <span class="keyword">instanceof</span> ItemHolder){</span><br><span class="line">        ItemHolder itemHolder  = (ItemHolder) holder;</span><br><span class="line">        itemHolder.tv_item_normal.setText(mitem_data.get(position -<span class="number">1</span> ).getTitle());</span><br><span class="line"></span><br><span class="line">        itemHolder.simpImg.setImageURI(mitem_data.get(position - <span class="number">1</span>).getThumbnail());</span><br><span class="line">        System.out.println(mitem_data.get(position - <span class="number">1</span>).getUrl()+<span class="string">&quot;----&gt;&#x56FE;&#x7247;&#x8DEF;&#x5F84;&quot;</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//&#x8FD4;&#x56DE;&#x6761;&#x76EE;&#x6570;&#x91CF;</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getItemCount</span><span class="params">()</span> </span>{</span><br><span class="line">    <span class="keyword">return</span> mitem_data.size()+<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* &#x544A;&#x8BC9;&#x8981;&#x521B;&#x5EFA;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x7684;ViewHolder*/</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getItemViewType</span><span class="params">(<span class="keyword">int</span> position)</span> </span>{</span><br><span class="line">    <span class="keyword">if</span> (position == <span class="number">0</span>){</span><br><span class="line">        <span class="keyword">return</span> TYPE_HEAD ;</span><br><span class="line">    }<span class="keyword">else</span>{</span><br><span class="line">        <span class="keyword">return</span> TYPE_NORMAL;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">  <span class="comment">//  &#x4E0B;&#x9762;&#x662F;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x9002;&#x914D;&#x5668;&#x7C7B;</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* &#x6B63;&#x5E38;&#x7684;item*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ItemHolder</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">ViewHolder</span></span>{</span><br><span class="line">    TextView tv_item_normal;</span><br><span class="line">    SimpleDraweeView simpImg;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ItemHolder</span><span class="params">(View itemView)</span> </span>{</span><br><span class="line">        <span class="keyword">super</span>(itemView);</span><br><span class="line">        tv_item_normal = (TextView) itemView.findViewById(R.id.tv_item_normal);</span><br><span class="line">        simpImg = (SimpleDraweeView) itemView.findViewById(R.id.simpImg);</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">* &#x6807;&#x9898;&#x8F6E;&#x64AD;&#x56FE;*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BannerHolder</span> <span class="keyword">extends</span> <span class="title">RecyclerView</span>.<span class="title">ViewHolder</span></span>{</span><br><span class="line"></span><br><span class="line">    Banner banner;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">BannerHolder</span><span class="params">(View itemView)</span> </span>{</span><br><span class="line">        <span class="keyword">super</span>(itemView);</span><br><span class="line">        banner = (Banner) itemView.findViewById(R.id.img_banner);</span><br><span class="line"></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="&#x63A5;&#x53E3;&#x56DE;&#x8C03;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;"><a href="#&#x63A5;&#x53E3;&#x56DE;&#x8C03;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;" class="headerlink" title="&#x63A5;&#x53E3;&#x56DE;&#x8C03;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;"></a>&#x63A5;&#x53E3;&#x56DE;&#x8C03;&#x7684;&#x7B80;&#x5355;&#x5199;&#x6CD5;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//&#x4E0B;&#x9762;&#x662F;&#x56DE;&#x6389;&#x63A5;&#x53E3;&#x7684;&#x5199;&#x6CD5;</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">MyItemClickListener</span></span>{</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnClick</span><span class="params">(View itemView, <span class="keyword">int</span> position)</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//&#x672C;&#x7C7B;&#x4E2D;&#x4FDD;&#x7559;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x7684;&#x5F15;&#x7528;</span></span><br><span class="line"></span><br><span class="line">MyItemClickListener listener;</span><br><span class="line"><span class="comment">//&#x5199;Set&#x65B9;&#x6CD5;</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">SetItemClickListener</span><span class="params">(MyItemClickListener listener)</span></span>{</span><br><span class="line">    <span class="keyword">this</span>.listener = listener ;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="&#x5F00;&#x6E90;&#x6846;&#x67B6;OkHttp&#x7684;&#x4F7F;&#x7528;"><a href="#&#x5F00;&#x6E90;&#x6846;&#x67B6;OkHttp&#x7684;&#x4F7F;&#x7528;" class="headerlink" title="&#x5F00;&#x6E90;&#x6846;&#x67B6;OkHttp&#x7684;&#x4F7F;&#x7528;"></a>&#x5F00;&#x6E90;&#x6846;&#x67B6;OkHttp&#x7684;&#x4F7F;&#x7528;</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//&#x5EFA;&#x7ACB;&#x7F51;&#x7EDC;&#x6846;&#x67B6;&#xFF0C;&#x83B7;&#x53D6;&#x7F51;&#x7EDC;&#x6570;&#x636E;</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getResult</span><span class="params">()</span></span>{</span><br><span class="line">        <span class="keyword">new</span> Thread(<span class="keyword">new</span> Runnable() {</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>{</span><br><span class="line">                <span class="comment">//&#x521B;&#x5EFA;&#x4E00;&#x4E2A;OKHttpClient&#x5BF9;&#x8C61;&#xFF0C;&#x4E00;&#x4E2A;app&#x91CC;&#x6700;&#x597D;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;&#x6B64;&#x5BF9;&#x8C61;</span></span><br><span class="line">                OkHttpClient client = <span class="keyword">new</span> OkHttpClient();</span><br><span class="line"></span><br><span class="line">                <span class="comment">//&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;</span></span><br><span class="line">                Request request = <span class="keyword">new</span> Request.Builder().url(<span class="string">&quot;http://news.ifeng.com/&quot;</span>).build();</span><br><span class="line"></span><br><span class="line">                <span class="comment">//&#x6267;&#x884C;&#x8BF7;&#x6C42;&#x83B7;&#x5F97;&#x54CD;&#x5E94;&#x7ED3;&#x679C;</span></span><br><span class="line">                Call call = client.newCall(request);</span><br><span class="line"></span><br><span class="line">                call.enqueue(<span class="keyword">new</span> Callback() {</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onFailure</span><span class="params">(Call call, IOException e)</span> </span>{</span><br><span class="line">                    }</span><br><span class="line"></span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onResponse</span><span class="params">(Call call, Response response)</span> <span class="keyword">throws</span> IOException </span>{</span><br><span class="line">                        <span class="keyword">if</span> (response.isSuccessful()) {</span><br><span class="line">                            <span class="comment">//&#x6267;&#x884C;&#x8BF7;&#x6C42;&#x6210;&#x529F;&#x7684;&#x64CD;&#x4F5C;</span></span><br><span class="line">                            String str = response.body().string();</span><br><span class="line"></span><br><span class="line"><span class="comment">//                            Log.i(&quot;&quot;, &quot;onResponse: &quot;+str);</span></span><br><span class="line">                            Message msg = <span class="keyword">new</span> Message();</span><br><span class="line">                            msg.obj = str;</span><br><span class="line">                            msg.what = <span class="number">0x001</span>;</span><br><span class="line">                            mHandler.sendMessage(msg);</span><br><span class="line"></span><br><span class="line">                        }<span class="keyword">else</span>{</span><br><span class="line">                           <span class="keyword">throw</span> <span class="keyword">new</span> IOException(<span class="string">&quot;&quot;</span>+response);</span><br><span class="line"></span><br><span class="line">                        }</span><br><span class="line">                    }</span><br><span class="line">                });</span><br><span class="line"></span><br><span class="line">            }</span><br><span class="line">        }).start();</span><br></pre></td></tr></table></figure>
<h2 id="&#x4F7F;&#x7528;Fragment&#x66FF;&#x6362;FramLayout"><a href="#&#x4F7F;&#x7528;Fragment&#x66FF;&#x6362;FramLayout" class="headerlink" title="&#x4F7F;&#x7528;Fragment&#x66FF;&#x6362;FramLayout"></a>&#x4F7F;&#x7528;Fragment&#x66FF;&#x6362;FramLayout</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">FragmentManager fm = getFragmentManager();   &#x83B7;&#x5F97;&#x7BA1;&#x7406;&#x5668;</span><br><span class="line">FragmentTransaction transaction = fm.beginTransaction();  &#x5F00;&#x542F;&#x4E8B;&#x7269;</span><br><span class="line">transaction.replace(R.id.main_news,<span class="keyword">new</span> news_home_Fragment());  &#x66FF;&#x6362;</span><br><span class="line">transaction.commit();  &#x63D0;&#x4EA4;</span><br></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Anderoid基础/" rel="tag"># Anderoid基础</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/30/Android基础（一）/" rel="next" title="Android基础（一）">
                <i class="fa fa-chevron-left"></i> Android基础（一）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/30/TextInputLayout/" rel="prev" title="TextInputLayout">
                TextInputLayout <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/me.jpg"
               alt="JokerLiang" />
          <p class="site-author-name" itemprop="name">JokerLiang</p>
          <p class="site-description motion-element" itemprop="description">永远年轻，永远热泪盈眶。</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">61</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/LoverJoker" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/lover-easy-7/activities" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/2292572142/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Friends
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.knight.ac.cn/" title="Knight" target="_blank">Knight</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://lzllzl.cn/" title="Zayvion" target="_blank">Zayvion</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://chen97.com/" title="Chens" target="_blank">Chens</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#封装SharedPreferences"><span class="nav-number">1.</span> <span class="nav-text">封装SharedPreferences</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#将流转换为字符串对象（用在解析JSON处-不适用Gson）"><span class="nav-number">2.</span> <span class="nav-text">将流转换为字符串对象（用在解析JSON处,不适用Gson）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#判断服务是否运行"><span class="nav-number">3.</span> <span class="nav-text">判断服务是否运行</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#仿广告轮播图，ViewPager的应用"><span class="nav-number">4.</span> <span class="nav-text">仿广告轮播图，ViewPager的应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备数据"><span class="nav-number">4.1.</span> <span class="nav-text">准备数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#写出设配器"><span class="nav-number">4.2.</span> <span class="nav-text">写出设配器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#添加滚动事件"><span class="nav-number">4.3.</span> <span class="nav-text">添加滚动事件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在子线程中实现自动轮播"><span class="nav-number">4.4.</span> <span class="nav-text">在子线程中实现自动轮播</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Handler定时器的用法"><span class="nav-number">5.</span> <span class="nav-text">Handler定时器的用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#TabLayout实现标题栏的简单使用"><span class="nav-number">6.</span> <span class="nav-text">TabLayout实现标题栏的简单使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RecyclerView适配器的简单写法"><span class="nav-number">7.</span> <span class="nav-text">RecyclerView适配器的简单写法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口回调的简单写法"><span class="nav-number">8.</span> <span class="nav-text">接口回调的简单写法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#开源框架OkHttp的使用"><span class="nav-number">9.</span> <span class="nav-text">开源框架OkHttp的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用Fragment替换FramLayout"><span class="nav-number">10.</span> <span class="nav-text">使用Fragment替换FramLayout</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">JokerLiang</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	




  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


</body>
</html>
